package router

import (
	"time"

	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"github.com/kataras/iris/sessions"

	"im/app/controller"
	"im/app/middlewares"
)

func AppRouter(app *iris.Application) {

	app.Get("/", func(context iris.Context) {
		context.View("index.html")
		return
	})

	// 注册视图
	app.RegisterView(iris.HTML("./app/view", ".html"))

	// app授权
	mvc.Configure(app.Party("/apps/token"), func(app *mvc.Application) {
		app.Handle(new(controller.AppsTokenController))
	})

	// 用户权限与注册相关接口
	mvc.Configure(app.Party("/users"), func(app *mvc.Application) {
		// 中间件
		app.Router.Use(middlewares.AppsCheck)

		// 依赖注入
		app.Register(
			sessions.New(sessions.Config{}).Start,
		)

		// 注册
		app.Handle(new(controller.UsersAccessController))

	})

	// 用户登录创建
	// mvc.Configure(app.Party("/users/token"), func(app *mvc.Application) {
	// 	app.Handle(new(controller.UsersTokenController))
	// })

	// 用户相关接口
	mvc.Configure(app.Party("/users"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.UsersController))
	})

	// 联系人相关接口
	mvc.Configure(app.Party("/friends"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)

		app.Handle(new(controller.FriendsController))
	})

	// 群聊相关接口
	mvc.Configure(app.Party("/groups"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.GroupsController))
	})

	// 聊天记录
	mvc.Configure(app.Party("/private_messages"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.PrivateMessagesController))
	})

	// 附件记录添加
	mvc.Configure(app.Party("/attachments"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.AttachmentsController))
	})

	// 帖子
	mvc.Configure(app.Party("/posts"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.Posts))
	})
	// 帖子
	mvc.Configure(app.Party("/contents/{id:string regexp(^[0-9_.-]+.html$))}"), func(app *mvc.Application) {
		app.Handle(new(controller.Contents))
	})
	// 系统消息
	mvc.Configure(app.Party("/systems"), func(app *mvc.Application) {
		app.Router.Use(middlewares.UserVerifyRongyun)
		app.Handle(new(controller.InternalMails))
		app.Handle(new(controller.Posts))
	})
	// 系统消息
	mvc.Configure(app.Party("/systems"), func(app *mvc.Application) {
		app.Handle(new(controller.ChannelsController))
	})

	//
	// same as app.Handle("GET", "/ping", [...])
	// Method:   GET
	// Resource: http://localhost:8080/ping
	app.Get("/ping", func(ctx iris.Context) {
		ctx.WriteString(time.Now().String())
	})
}
